import 'package:agora_rtc_engine/rtc_engine.dart';
import 'package:flutter/material.dart';
// import 'package:flutter_application_1/models/camera_model.dart';
import 'package:agora_rtc_engine/rtc_local_view.dart' as RtcLocalView;
import 'package:flutter_application_cyber/model/cn_net_response.dart';
import 'package:flutter_application_cyber/model/cn_user_default_cache.dart';
// import 'package:flutter_application_1/pool/constant_pool.dart';
import 'package:flutter_application_cyber/model/cn_strategy_vmodel.dart';
// import 'package:flutter_application_1/view_model/video_vmodel.dart';
import 'package:flutter_util_code/toast_utils.dart';
import 'package:provider/provider.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:flutter_application_cyber/pages/Mate/Models/cn_channel_model.dart';

/*
*
* 本地视频组件
* */
class CnMineVideoView extends StatefulWidget {
  final CnChannelModel? channelModel;

  final Function() openCamera;

  final int uid;

  const CnMineVideoView(this.channelModel, this.uid,
      {super.key, required this.openCamera});

  @override
  State<StatefulWidget> createState() => MineVideoState();
}

class MineVideoState extends State<CnMineVideoView> {
  late Offset position;
  var selfVideoHeight = 161; //本地视频高度
  var selfVideoWight = 123; //本地视频宽度
  var isFirstOpenCamera = false;
  late Size screenSize;
  // CameraModel? cameraModel;

  @override
  void initState() {
    super.initState();
    getIsFirstOpenCamera();
    getCameraConfig();
  }

  @override
  Widget build(BuildContext context) {
    return Positioned(
        left: position.dx,
        top: 90,
        child: GestureDetector(
          child: SizedBox(
              width: 130,
              height: 190,
              child: Stack(fit: StackFit.expand, children: [
                ClipRRect(
                    borderRadius: BorderRadius.circular(10), // 设置圆角半径
                    child: widget.uid == 1
                        ? Container(color: Colors.black)
                        : const RtcLocalView.SurfaceView()),
                // Positioned(
                //     bottom: 0,
                //     right: 0,
                //     child: InkWell(
                //         onTap: () {

                //         },
                //         child: SizedBox(
                //             height: 24,
                //             width: 24,
                //             child: Stack(fit: StackFit.expand, children: [
                //               Image.asset('images/ic_camera.webp'),
                //               Positioned(
                //                   top: 0,
                //                   left: 0,
                //                   child: Visibility(
                //                       visible: isFirstOpenCamera,
                //                       child: Container(
                //                           height: 6,
                //                           width: 6,
                //                           decoration: BoxDecoration(
                //                               color: Colors.red,
                //                               borderRadius:
                //                                   BorderRadius.circular(50)))))
                //             ]))))
              ])),
          onPanUpdate: (offset) {
            double offsetX = offset.delta.dx + position.dx;
            double offsetY = offset.delta.dy + position.dy;
            if (offsetX < 0) {
              offsetX = 0;
            }
            if (offsetX > screenSize.width - selfVideoWight) {
              offsetX = screenSize.width - selfVideoWight;
            }
            if (offsetY < 0) {
              offsetY = 0;
            }
            if (offsetY > screenSize.height - selfVideoHeight) {
              offsetY = screenSize.height - selfVideoHeight;
            }

            setState(() {
              position = Offset(offsetX, offsetY);
            });
          },
        ));
  }

  @override
  void didChangeDependencies() {
    super.didChangeDependencies();
    screenSize = MediaQuery.of(context).size; //获取屏幕尺寸
    position = Offset(screenSize.width - selfVideoWight - 20, 74); //初始偏移量
  }

  void getIsFirstOpenCamera() async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    // var value = prefs.getBool(ConstantPool.isFirstOpenCamera) ?? true;
    // setState(() {
    //   isFirstOpenCamera = value;
    // });
  }

  UserInfoModel getMineInfo() {
    return Provider.of<ChangeUserInfoModel>(context, listen: false).info;
  }

  void getCameraConfig() {
    // VideoViewModel.getInfo().getCameraConfig((CameraModel? value) {
    //   setState(() {
    //     cameraModel = value;
    //   });
    // });
  }
}
